package com.apache.passport.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MapTools {

	public static boolean setTran(Map<String, ArrayList<String>> tranMap, String textName, String textValue) {
		boolean result = true;
		Object obj = tranMap.get(textName);
		ArrayList<String> tranValue = null;
		if (obj != null) {
			tranValue = (ArrayList<String>) tranMap.get(textName);
		} else {
			tranValue = new ArrayList<String>();
		}
		tranValue.add(textValue);
		tranMap.put(textName, tranValue);

		return result;
	}

	public static boolean setTran2(Map<String, ArrayList<String>> tranMap, String textName, String textValue) {
		boolean result = true;
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue.add(textValue);
		tranMap.put(textName, tranValue);

		return result;
	}

	public static String getTran(Map<String, ArrayList<String>> tranMap, String textName) {
		String textValue = "";
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = tranMap.get(textName);
		if (tranValue != null) {
			textValue = tranValue.get(0);
		}
		return textValue;
	}

	public static boolean setData(Map<String, ArrayList<String>> dataMap, String textName, String textValue) {
		boolean result = true;
		Object obj = dataMap.get(textName);
		ArrayList<String> tranValue = null;
		if (obj != null) {
			tranValue = (ArrayList<String>) dataMap.get(textName);
		} else {
			tranValue = new ArrayList<String>();
		}
		if (tranValue.size() == 1) {
			tranValue.clear();
		}
		tranValue.add(textValue);
		dataMap.put(textName, tranValue);
		return result;
	}

	public static boolean setData3(Map<String, ArrayList<String>> dataMap, String textName, String textValue) {
		boolean result = true;
		Object obj = dataMap.get(textName);
		ArrayList<String> tranValue = null;
		if (obj != null) {
			tranValue = (ArrayList<String>) dataMap.get(textName);
		} else {
			tranValue = new ArrayList<String>();
		}
		tranValue.add(textValue);
		dataMap.put(textName, tranValue);
		return result;
	}

	public static boolean setData2(Map<String, ArrayList<String>> dataMap, String textName, String textValue) {
		boolean result = true;
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue.add(textValue);
		dataMap.put(textName, tranValue);
		return result;
	}

	public static String getData(Map<String, ArrayList<String>> dataMap, int index, String textName) {
		String textValue = "";
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = dataMap.get(textName);
		if (tranValue != null) {
			textValue = tranValue.get(index);
		}
		return textValue;
	}

	/**
	 * 新增2013年6月18日12:57:03
	 * @param dataMap
	 * @param textName
	 * @param index
	 * @return
	 */
	public static String getData(Map<String, ArrayList<String>> dataMap, String textName, int index) {
		String textValue = "";
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = dataMap.get(textName);
		if (tranValue != null) {
			textValue = tranValue.get(0);
		}
		return textValue;
	}

	/**
	 * 新增 2014年5月20日10:17:46 中信
	 * @param dataMap
	 * @param textName
	 * @param index
	 * @return
	 */
	public static String getData3(Map<String, ArrayList<String>> dataMap, String textName) {
		String textValue = "";
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = dataMap.get(textName);
		StringBuffer sb = new StringBuffer();
		if (tranValue.size() > 0) {
			for (int i = 0; i < tranValue.size(); i++) {
				String value = tranValue.get(i);
				if (i == tranValue.size() - 1) {
					sb.append(value);
				} else {
					sb.append(value + ",");
				}
			}
		}
		textValue = sb.toString();
		return textValue;
	}

	public static boolean setData1(Map<String, String> dataMap, String textName, String textValue) {
		boolean result = true;
		if (textValue == null) {
			textValue = "";
		}
		dataMap.put(textName, textValue);

		return result;
	}

	public static String getData1(Map<String, String> dataMap, String textName) {
		String textValue = "";
		textValue = dataMap.get(textName);
		if (textValue == null) {
			textValue = "";
		}
		return textValue;
	}

	public static String getTranTxCodeFromData(Map<String, ArrayList<String>> dataMap) {
		String textValue = "";
		textValue = getData(dataMap, 0, "TranTxCode");
		if (textValue == null) {
			textValue = "";
		}
		return textValue;
	}

	public static String getChannelFromData(Map<String, ArrayList<String>> dataMap) {
		String textValue = "";

		textValue = getData(dataMap, 0, "Channel");
		if (textValue == null) {
			textValue = "";
		}
		return textValue;
	}

	public static String getBankTxCodeFromData(Map<String, ArrayList<String>> dataMap) {
		String textValue = "";
		textValue = getData(dataMap, 0, "BankTxCode");
		if (textValue == null) {
			textValue = "";
		}
		return textValue;
	}

	public static String getTranTxCodeFromTranMap(Map<String, ArrayList<String>> tranMap) {
		String textValue = "";
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = tranMap.get("TranTxCode");
		if (tranValue != null) {
			textValue = tranValue.get(0);
		}
		return textValue;
	}

	public static String getBankTxCodeFromTranMap(Map<String, ArrayList<String>> tranMap) {
		String textValue = "";
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = tranMap.get("BankTxCode");
		if (tranValue != null) {
			textValue = tranValue.get(0);
		}
		return textValue;
	}

	public static String getChannelFromTranMap(Map<String, ArrayList<String>> tranMap) {
		String textValue = "";
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = tranMap.get("Channel");
		if (tranValue != null) {
			textValue = tranValue.get(0);
		}
		return textValue;
	}

	public static String getData(Map<String, ArrayList<String>> tranMap, String string) {
		return getData(tranMap, 0, string);
	}

	public static String getData2(Map<String, ArrayList<String>> dataMap, String textName) {
		String textValue = "";
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = dataMap.get(textName);
		if (tranValue != null) {
			textValue = tranValue.get(0);
		}
		return textValue;
	}

	public static boolean removeKey(Map<String, ArrayList<String>> dataMap, String textName) {
		boolean isRemove = true;
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue = dataMap.remove(textName);
		if (tranValue != null) {
			isRemove = false;
		}
		return isRemove;
	}

	public static void main(String[] args) {
		Map<String, ArrayList<String>> dataMap = new HashMap<String, ArrayList<String>>();
		ArrayList<String> tranValue = new ArrayList<String>();
		tranValue.add("123");
		tranValue.add("1234");
		dataMap.put("list1", tranValue);
		dataMap.put("list2", tranValue);
		dataMap.put("list3", tranValue);
		dataMap.put("list3", tranValue);
		MapTools.removeKey(dataMap, "list2");
		System.out.println(dataMap.size());
	}
}
